package org.rcgwt.client.ui.validator.messages;

import org.rcgwt.client.ui.validator.events.MessageEvent;
import org.rcgwt.client.ui.validator.events.MessageHandler;

import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A component that allows a message display
 * 
 * @author Elian ORIOU
 * 
 */

public class MessageDisplay extends Composite implements IMessageDisplay {

	private Widget owner;
	private Label message;
	private Message msg;

	/* State booleans */
	private boolean isSummaryDisplayed;
	private boolean isDetailDisplayed;
	private boolean isSeverityDisplayed;

	/* Global Error Display Only */
	private boolean isGlobalOnly;

	public MessageDisplay() {
		RootPanel rp = RootPanel.get();
		message = new Label();
		rp.add(message);

		isGlobalOnly = false;

		isSummaryDisplayed = true; // By default the MessageDisplay component
		// displays the message summary
		isDetailDisplayed = false;
		isSeverityDisplayed = false;

		/* The component can receive MessageEvent */
		this.addMessageHandler(new MessageHandler() {
			@Override
			public void onMessageEvent(MessageEvent event) {
				Message e = event.getMessage();
				setMessage(e);
			}
		});
		/* The message detail appears when the cursor enters on the component */
		message.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				if (msg == null) {
					return;
				}
				message.setTitle(msg.getDetails());
			}
		});
		initWidget(message);
	}

	/**
	 * Sets the message to display to <i>m</i>
	 * 
	 * @param m
	 *            The message
	 */

	@Override
	public void setMessage(Message m) {
		msg = m;
		String severity = "";
		String details = "";
		String summary = "";
		if (isSeverityDisplayed == true) {
			severity = m.getSeverity() + " : ";
		}
		if (isDetailDisplayed == true) {
			details = m.getDetails();
		}
		if (isSummaryDisplayed == true) {
			summary = " (" + m.getSummary() + ")";
		}
		message.setText(severity + details + summary);
	}

	public Widget getFor() {
		return owner;
	}

	public void setFor(Widget owner) {
		this.owner = owner;
	}

	public Label getMessage() {
		return message;
	}

	public void displaySummary(boolean ds) {
		this.isSummaryDisplayed = ds;
	}

	public void displayDetail(boolean dd) {
		this.isDetailDisplayed = dd;
	}

	public void displaySeverity(boolean ds) {
		this.isSeverityDisplayed = ds;
	}

	public boolean isGlobalOnly() {
		return isGlobalOnly;
	}

	/**
	 * Sets the global only behavior (the component displays global message
	 * only)
	 */

	public void setGlobalOnly(boolean isGlobalOnly) {
		this.isGlobalOnly = isGlobalOnly;
	}

	@Override
	public void clearMessage() {
		message.setText("");
	}

	@Override
	public String toString() {
		return "MessageDisplay [owner=" + owner + "]";
	}

	@Override
	public HandlerRegistration addMessageHandler(MessageHandler handler) {
		return addHandler(handler, MessageEvent.getType());
	}
}